package servlet;

import pojo.Anime;
import pojo.Ratingrecord;
import service.UserService;
import service.implement.UserServiceImplement;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.List;

public class UserServlet extends BaseServlet {
    private String user_record="";

    private UserService userService=new UserServiceImplement();


    //处理登陆逻辑

    protected void login(HttpServletRequest request,HttpServletResponse response)
    {
        //获取登陆id
        String userId=request.getParameter("user_id");

        user_record=userId;
        if(userService.isExistId(userId)){
            System.out.println("登陆成功");
            try {
                request.getRequestDispatcher("/pages/mainMenu.jsp").forward(request,response);
            } catch (ServletException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }else{
            System.out.println("当前用户id不存在或不合法");
            request.setAttribute("msg","当前用户id不存在或者不合法");
            request.setAttribute("user_ID",userId);
            try {
                request.getRequestDispatcher("/index.jsp").forward(request,response);
            } catch (ServletException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }



    //处理注册逻辑

    protected void register(HttpServletRequest request,HttpServletResponse response)
    {
        String user_ID=userService.Provideregister();
        System.out.println("当前分配的uid:"+user_ID);

        try {
            request.setAttribute("user_id",user_ID);
            request.getRequestDispatcher("/pages/register.jsp").forward(request,response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }



    //处理评分逻辑
    protected void rate(HttpServletRequest request, HttpServletResponse response)
    {

        String anime_Id=request.getParameter("anime_id");
        String rat=request.getParameter("rating");


        int rat_cal=Integer.parseInt(rat);

        String result=userService.rate(user_record,anime_Id,rat_cal);
        request.setAttribute("ratFeedback",result);

        try {
            request.getRequestDispatcher("/pages/mainMenu.jsp").forward(request,response);
        } catch (ServletException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }


    //处理推荐逻辑

    protected void recommendation(HttpServletRequest request,HttpServletResponse response)
    {
        List<Anime>animeList=userService.recommendationList(user_record);
        request.setAttribute("animeList",animeList);
        try {
            request.getRequestDispatcher("/pages/category.jsp").forward(request,response);
        } catch (ServletException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }



    //处理查询逻辑

    protected  void  doInfo(HttpServletRequest request,HttpServletResponse response)
    {
        System.out.println("用户查询信息");
        List<Ratingrecord> ratingrecords=userService.recordHistory(user_record);

        request.setAttribute("recordHistory",ratingrecords);

        try {
            request.getRequestDispatcher("/pages/rating_Record.jsp").forward(request,response);
        } catch (ServletException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }




}
